package ide.ccf.ccproject.wizards.pages;

import ide.ccf.ccproject.ProjectPlugin;
import ide.ccf.ccproject.common.Utils;
import ide.ccf.ccproject.preferences.PreferenceConstants;

import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;


/**
 * CCF版本向导页面
 *
 */
public class VersionWizardPage extends WizardPage
{
	//Text versionName;
	Combo versionName;
	DirectoryFieldEditor binPath;
	DirectoryFieldEditor includePath;
	String errorMessage = "";
	public static boolean isValid = false;
	
	/**
	 * 构造器
	 * @param pageName
	 */
	public VersionWizardPage(String pageName) 
	{
		super(pageName);
		setTitle("CCF版本");
		setDescription("指定CCF的版本、Bin路径和Include路径。");
	}

	/**
	 * 创建页面显示控制
	 */
	public void createControl(Composite parent) 
	{
		Composite composite =  new Composite(parent, SWT.NULL);

		GridLayout gl = new GridLayout();
		int ncol = 3;
		gl.numColumns = ncol;
		composite.setLayout(gl);

		Label versionNameLabel = new Label(composite, SWT.NULL);
		versionNameLabel.setText("CCF版本:");
		versionName = new Combo(composite, SWT.BORDER | SWT.SINGLE);
		versionName.addModifyListener(new ModifyListener() 
		{
			public void modifyText(ModifyEvent e) 
			{
				dialogChangedByVersionName();
			}
		});
		
		GridData versionNameGridData = new GridData(GridData.FILL_HORIZONTAL);
		versionName.setLayoutData(versionNameGridData);
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);
		
		binPath = new DirectoryFieldEditor("bin", "Bin路径:", composite);
		binPath.getTextControl(composite).addModifyListener(new ModifyListener() 
		{
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		binPath.setPropertyChangeListener(new IPropertyChangeListener() 
		{
			public void propertyChange(PropertyChangeEvent event) 
			{
				pathSelected(Utils.PATH_TYPE_BIN);
			}
		});
		new Label(composite, SWT.NULL);
		versionName.setLayoutData(versionNameGridData);
		Label binPathDescription = new Label(composite, SWT.NULL);
		binPathDescription.setText("包含CCF所有可执行文件的目录。\n");
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);
		
		includePath = new DirectoryFieldEditor("include", "Include路径:", composite);
		includePath.getTextControl(composite).addModifyListener(new ModifyListener() 
		{
			public void modifyText(ModifyEvent e) 
			{
				dialogChanged();
			}
		});
		includePath.setPropertyChangeListener(new IPropertyChangeListener() 
		{
			public void propertyChange(PropertyChangeEvent event) 
			{
				pathSelected(Utils.PATH_TYPE_INCLUDE);
			}
		});
		new Label(composite, SWT.NULL);
		Label includePathDescription = new Label(composite, SWT.NULL);
		includePathDescription.setText("包含CCF开发库头文件的路径。");
		new Label(composite, SWT.NULL);
		
		String[] strCCFVersions = getCCFVersions();
		versionName.setItems(strCCFVersions);
		for(int i = 0; i < strCCFVersions.length; i++)
			versionName.setData(strCCFVersions[i]);
		versionName.setText(strCCFVersions[0]);
		
		setControl(composite);
		setPageComplete(isValid());
	}

	/**
	 * 判断版本名称合法性
	 * @return
	 */
	private boolean isVersionNameValid() 
	{
		return versionName.getText().length() > 0;
	}
	
	/**
	 * 判断Bin路径合法性
	 * @return
	 */
	private boolean isBinPathValid() 
	{
		return Utils.isValidCCFPath(binPath.getStringValue(), Utils.PATH_TYPE_BIN);
	}
	
	/**
	 * 判断Include路径合法性
	 * @return
	 */
	private boolean isIncludePathValid() 
	{
		return Utils.isValidCCFPath(includePath.getStringValue(), Utils.PATH_TYPE_INCLUDE);
	}
	
	/**
	 * 判断页面所有数据是否正确
	 * @return
	 */
	private boolean isValid() 
	{
		return (isVersionNameValid() && isBinPathValid() && isIncludePathValid());
	}

	/**
	 * 对话框变化事件处理
	 */
	public void dialogChanged() 
	{
		isValid = isValid();
		errorMessage = "";
		if (!isValid) 
		{
			if (!isBinPathValid())
				errorMessage += "Bin路径不合法。 ";
			else
				setErrorMessage(null);
			
			if (!isIncludePathValid())
				errorMessage += "Include路径不合法。";
			else
				setErrorMessage(null);
			
			setErrorMessage(errorMessage);
		} 
		else 
		{
			setErrorMessage(null);
		}
		setPageComplete(isValid);
	}
	
	/**
	 * 对话框变化事件处理
	 */
	public void dialogChangedByVersionName() 
	{
		isValid = isValid();
		errorMessage = "";
		if (!isValid) 
		{		
			if (!isVersionNameValid())
			{
				errorMessage += "CCF版本为空。";
				setErrorMessage(errorMessage);
			}
			else
				setErrorMessage(null);
		} 
		else 
		{
			setErrorMessage(null);
		}
		setPageComplete(isValid);
	}
	
	/**
	 * 选择路径
	 * @param pathType
	 */
	public void pathSelected(int pathType) 
	{
		int siblingPathType = pathType == Utils.PATH_TYPE_BIN?Utils.PATH_TYPE_INCLUDE:Utils.PATH_TYPE_BIN;
		String selectedPathEntry = (pathType == Utils.PATH_TYPE_BIN ? binPath : includePath).getStringValue();
		String siblingPathEntry = (pathType == Utils.PATH_TYPE_BIN ? includePath : binPath).getStringValue();
		boolean selectedPathEntryIsValid = Utils.isValidCCFPath(selectedPathEntry, pathType);
		boolean siblingPathEntryIsEmpty = siblingPathEntry.length() == 0;

		if (!selectedPathEntryIsValid) 
		{
			String investigatedPath = Utils.getCCFSubPathUnderCCFPath(selectedPathEntry, pathType);
			
			if (investigatedPath.length() > 0) 
			{
				selectedPathEntry = investigatedPath;
				selectedPathEntryIsValid = true;
			}
		}
		
		if (selectedPathEntryIsValid && siblingPathEntryIsEmpty)
			siblingPathEntry = Utils.getSiblingCCFPath(selectedPathEntry, pathType, siblingPathType);
		
		(pathType == Utils.PATH_TYPE_BIN ? binPath : includePath).setStringValue(selectedPathEntry);
		(siblingPathType == Utils.PATH_TYPE_BIN ? binPath : includePath).setStringValue(siblingPathEntry);
	}
	
	/**
	 * 获取版本名称
	 * @return
	 */
	public String getVersionName() 
	{
		return versionName.getText();
	}

	/**
	 * 设置版本名称
	 * @param versionName
	 */
	public void setVersionName(String versionName) 
	{
		this.versionName.setText(versionName);
	}

	private String[] getCCFVersions() {
		if(ProjectPlugin.getDefault() != null)
		{
			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			
			if (!store.contains(PreferenceConstants.CCF_VERSIONS))
				return null;
			
			String defaultVersions = store.getString(PreferenceConstants.CCF_VERSIONS);
			defaultVersions = defaultVersions.substring(1, defaultVersions.length()-1);
			//System.out.println("defaultVersions:"+defaultVersions);
			//cut string
			String[] b = defaultVersions.split(",");
			if(b.length > 0)
				return b;	
		}
		return null;
	}

	/**
	 * 获取Bin路径
	 * @return
	 */
	public String getBinPath() 
	{
		return binPath.getStringValue();
	}

	/**
	 * 设置Bin路径
	 * @param binPath
	 */
	public void setBinPath(String binPath) 
	{
		this.binPath.setStringValue(binPath);
		dialogChanged();
	}

	/**
	 * 获取Include路径
	 * @return
	 */
	public String getIncludePath() 
	{
		return includePath.getStringValue();
	}

	/**
	 * 设置Include路径
	 * @param includePath
	 */
	public void setIncludePath(String includePath) 
	{
		this.includePath.setStringValue(includePath);
		dialogChanged();
	}
}
